<?php
class CitiesController extends AppController {
	var $name = 'Cities';
	function beforeFilter(){
		parent::beforeFilter();
		if (substr($this->params['action'], 0, 6) == 'admin_')
			$this->layout = "admin";
		else $this->layout = "lam";
	}
	function admin_index() {
		$this->paginator = array('limit' => 20);
		$this->set('cities',$this->paginate());
	}
	function admin_add() {
		if (!empty($this->data)) {
			$this->City->create();
			if ($this->City->save($this->data)) {
				$this->Session->setFlash(__('The city has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The city could not be saved. Please, try again.', true));
			}
		}
	}
	function admin_edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid city', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->City->save($this->data)) {
				$this->Session->setFlash(__('The city has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The city could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->City->read(null, $id);
		}
	}
	function admin_delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for city', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->City->delete($id)) {
			$this->Session->setFlash(__('City deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('City was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
	
	/**
	* Lấy tất cả city để hiển thị
	*/
	function get_all_cities(){
		$this->City->recursive = 0;
		return $this->City->find('all');
	}
	/**
	* Lấy tên của City theo id
	* Được dùng để hiển thị tên thay vì id, ví dụ trong users/view
	*/
	function get_city_name($id=null){
		if(!$id) return "";
		else{
			$this->City->recursive = 0;
			$City = $this->City->read(null, $id);
			return $City['City']['name'];
		}
	}
	
}
?>